#include <iostream>
#include <stack>
#include <string>
using namespace std;
class Solution {
public:
    string decodeString(string s) {
        stack<int> countstack;
        stack<string> stringstack;
        string currentstring="";
        int currentcount=0;
        for (char a : s) {
            if (isdigit(a)) {
                currentcount = currentcount * 10 + (a-'0');
            }
            else if (a == '[') {
                countstack.push(currentcount);
                stringstack.push(currentstring);
                currentcount = 0;
                currentstring = "";
            }
            else if (a == ']') {
                int count = countstack.top();
                countstack.pop();
                string prevString = stringstack.top();
                stringstack.pop();
                string temp;
                for (int i = 0; i < count; i++) {
                    temp += currentstring;
                }
                currentstring = prevString + temp;
            }
            else {
                currentstring = currentstring + a;
            }
        }
        return currentstring;
    }
};